python ping模块 |
您所在的位置:网站首页 › python3 queue模块 › python ping模块 |
此功能适用于任何操作系统(Unix,Linux,macOS和Windows) Python 2和Python 3 编辑: 由@radato os.system取代subprocess.call。 通过@Borissubprocess.run()如果您使用的是Python 3.5+,则文档建议使用。import platform # For getting the operating system nameimport subprocess # For executing a shell commanddef ping(host): """ Returns True if host (str) responds to a ping request. Remember that a host may not respond to a ping (ICMP) request even if the host name is valid. """ # Option for the number of packets as a function of param = '-n' if platform.system().lower()=='windows' else '-c' # Building the command. Ex: "ping -c 1 google.com" command = ['ping', param, '1', host] return subprocess.call(command) == 0 请注意,根据Windows上的@ikrase,True如果出现Destination Host Unreachable错误,此功能仍将返回。 说明 该命令适用ping于Windows和类Unix系统。 选项-n(Windows)或-c(Unix)控制在此示例中设置为1的数据包数。 platform.system()返回平台名称。防爆。'Darwin'在macOS上。 subprocess.call()执行系统调用。防爆。subprocess.call(['ls','-l'])。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |